package net.mikoo.seals.boss.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/admin/${info.variableName}")
public class ${info.entityName}Controller extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(${info.entityName}Controller.class);
	
	@Autowired
	private ${info.entityName}Services ${info.variableName}Services;
	
	@RequestMapping("/list")
	public String list(${info.entityName}Query query, HttpServletRequest request, Model model) {
		PageModel<${info.entityName}> pm  = ${info.variableName}Services.getPageByQuery(query);
		model.addAttribute("pm", pm);
		return resultView(ADMIN_VIEW, "${info.variableName}/list");
	}
	

	@RequestMapping("/form")
	public String form() {
		
		return resultView(ADMIN_VIEW, "${info.variableName}/form");
	}
	
	
	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(${info.entityName} ${info.variableName},HttpServletRequest request, Model model) {
		try {
			${info.variableName}Services.save(${info.variableName});
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}

	@RequestMapping("/edit")
	public String edit(Long ${info.variableName}Id, Model model) {
		${info.entityName} ${info.variableName} = ${info.variableName}Services.getInfoById(${info.variableName}Id);
		model.addAttribute("${info.variableName}", ${info.variableName});
		return resultView(ADMIN_VIEW, "${info.variableName}/edit");
	}
	
	
	@RequestMapping("/modify")
	public Map<String,Object> modify(${info.entityName} ${info.variableName}, Model model) {
		
		try {
			${info.variableName}Services.modify(${info.variableName});
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
		
	}

}
